/**
 * Project Name:YCApiServer-Api
 * File Name:UserCenterService.java
 * Package Name:com.yc.music.service.service103
 * Date:2016年5月10日下午1:25:36
 * Copyright (c) 2016 
 *
 */
package com.yc.music.service.service21;

import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.validation.BindingResult;

import com.yc.music.common.context.YcContext;
import com.yc.music.common.util.BindingResultUtil;
import com.yc.music.context.YtConstant;
import com.yc.music.mapper.cache.MusicActivityCacheMapper;
import com.yc.music.mapper.dao.MusicActivityFlowMapper;
import com.yc.music.model.activity.MusicActivityFlow;
import com.yc.music.model.activity.MusicActivityVo;

/**
 * 
 * ClassName: MusicActivityFlowService <br/>
 * Function: 活动流水. <br/>
 * Reason: TODO ADD REASON(可选). <br/>
 * date: 2016年8月15日 下午2:32:17 <br/>
 *
 * @author qinhaiguan
 * @version
 */
@Service
public class MusicActivityFlowService {

	@Value("${Pic_QiLiu}")
	private String pIC_QiLiu;
	
	@Value("${Audio_QiLiu}")
	private String Audio_QiLiu;
	
	@Value("${audio_buckname}")
	private String audio_buckname;
	
	@Value("${pic_buckname}")
	private String pic_buckname;
	
	@Autowired
	private MusicActivityFlowMapper musicActivityFlowMapper;
	
	@Autowired
	private MusicActivityCacheMapper musicActivityCacheMapper;

	
	public Map<String,Object> attendActivity(MusicActivityFlow musicActivityFlow,Map<String,Object> result,BindingResult binding) throws Exception {
		String valiterMsg = BindingResultUtil.getBindErrorMsg(binding);
		if(StringUtils.isNotBlank(valiterMsg)) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, valiterMsg);
			result.put(YcContext.DATA,YcContext.EMPTY_STRING);
			return result;
		}
		//先查询
		MusicActivityVo musicActivityVo=musicActivityCacheMapper.getMusicActivityById(musicActivityFlow.getActivityid());
		if (null==musicActivityVo) {
			result.put(YcContext.CODE, 400);
			result.put(YcContext.MESSAGE, YtConstant.ACTIVITY_IS_NOT_EXIST);
			result.put(YcContext.DATA,YcContext.EMPTY_STRING);
			return result;
		}
		//先查询是否参加过该活动
		MusicActivityFlow flow=musicActivityFlowMapper.getActivityFlowById(musicActivityFlow.getActivityid(),musicActivityFlow.getUserid());
		
		if(null!=flow){
			//参加过则直接覆盖作品id
			musicActivityFlowMapper.updateActivityFlow(musicActivityFlow);
			result.put(YcContext.CODE, 200);
			result.put(YcContext.MESSAGE, YtConstant.SYS_SUCCSS_MSG);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
			return result;
			
		}else{
			//设置活动类型
			if (0==musicActivityVo.getType()) {
				musicActivityFlow.setType(1);  //歌曲
			}else{
				musicActivityFlow.setType(2);  //歌词
			}
			
			musicActivityFlowMapper.saveActivityFlow(musicActivityFlow);
			result.put(YcContext.CODE, 200);
			result.put(YcContext.MESSAGE, YtConstant.SYS_SUCCSS_MSG);
			result.put(YcContext.DATA, YcContext.EMPTY_STRING);
			return result;
		}
		
	}
	
}
